<?php

namespace App\Http\Controllers\Weixin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

use App\Repositories\Eloquent\Biz\WeixinRepository as Weixin;
use App\Repositories\Eloquent\Mgt\UtilRepository as Util;
use App\Repositories\Eloquent\Mgt\BookRepository as Book;

use App\Repositories\Eloquent\Edu\TypeRepository as Type;
use App\Repositories\Eloquent\Edu\DeployRepository as Deploy;
use App\Repositories\Eloquent\Edu\TuserRepository as Tuser;
use App\Repositories\Eloquent\Edu\ClaseRepository as Clase;

class WxController extends Controller
{

    protected $type, $deploy, $tuser;

    protected $wx, $book, $util;

    public function __construct(Type $type, Deploy $deploy, Tuser $tuser, Clase $clase,
                Weixin $wx, Book $book, Util $util)
    {
        parent::__construct();

        $this->type = $type;

        $this->deploy = $deploy;

        $this->tuser = $tuser;

        $this->clase = $clase;

        $this->wx = $wx;

        $this->book = $book;

        $this->util = $util;
    }


    public function index(Request $request)
    {
        $user = \Auth::guard()->user();

        $typies = $this->type->index('loader');

        if (!!$user) {
            $user->load(['books','clases']);
        }

        return view($this->view, compact('user', 'typies'));
    }



    public function deploy($tid)
    {
        $type = $this->type->show($tid);

        if ($type instanceof JsonResponse && $type->getStatusCode() === 404) {
            return abort('404');
        }

        $user = \Auth::guard()->user();

        $deploy = $this->deploy->achieve($tid);

        return view($this->view,compact('user','deploy','type'));
    }



    public function sign(Request $request)
    {
        return $this->tuser->sign($request);
    }


    public function sms(Request $request)
    {
        return $this->util->sendSms($request);
    }

    public function csms(Request $request)
    {
        return $this->util->sendSms($request,'cslnbj');
    }


    public function books(Request $request)
    {
        $books = $this->book->index()->paginate(50);

        return view($this->view, compact('books'));
    }


    public function covid(Request $request)
    {
        $students = \Config::get('covid');
        //dd($students);
        return view($this->view, compact('students'));
    }

    /**
     * 班级备注信息
     * @param  Request $request [description]
     * @param  [type]  $cid     [description]
     * @return [type]           [description]
     */
    public function claseMemo(Request $request,$cid)
    {
        return $this->clase->memo($cid);
    }



    public function postReg(Request $request)
    {
        //dd($request->gendar);
        return $this->wx->register($request);
    }


    public function postLogin(Request $request)
    {
        return $this->wx->login($request);
    }


    public function login()
    {
        return view($this->view);
    }


    public function reg()
    {
        return view($this->view);
    }

    public function logout()
    {
        \Auth::guard()->logout();

        return \Redirect::to('/');
    }

}